﻿using System.Windows.Navigation;
using CuteRibs.MVVMLib;
using CuteRibs.WowArmory.Models;
using CuteRibs.WowArmory.Resources;

namespace CuteRibs.WowArmory.ViewModels
{
	public class MainPageViewModel : PageViewModelBase
	{
		private bool isRegionSelected;
		private Region[] hosts;
		private RegionData regionData;

		public Region[] Hosts
		{
			get
			{
				if (this.hosts == null)
				{
					this.hosts = new Region[]
					{ 
						Region.Americas,
						Region.Europe,
						Region.Korea,
						Region.Taiwan,
						Region.China
					};
				}

				return this.hosts;
			}
		}

		public RegionData RegionData
		{
			get { return this.regionData; }
			set
			{
				this.regionData = value;
				this.NotifyPropertyChanged("RegionData");
			}
		}

		public AppRes AppRes { get { return App.AppResource; } }
		public AppTitleControlViewModel AppTitle { get; set; }

		public MainPageViewModel()
		{
			this.AppTitle = new AppTitleControlViewModel();

			if (App.IsDesignMode)
			{
				this.SelectRegion(Region.Americas);
			}
		}

		public override void OnNavigatedTo(ViewBase sender, NavigationEventArgs eventArgs, NavigationContext context)
		{
			base.OnNavigatedTo(sender, eventArgs, context);

			if (this.isRegionSelected)
			{
				this.RegionData = App.RegionData;
			}
		}

		public void SelectRegion(Region region)
		{
			this.isRegionSelected = true;
			App.SelectRegion(region);
			this.RegionData = App.RegionData;
		}

		public void Navigate(string key)
		{
			var host = this.RegionData.Region.Domain;
			string uri = null;

			switch (key)
			{
				case "realms":
					uri = "/Views/RealmsPageView.xaml";
					break;
				case "characters":
					uri = "/Views/CharactersPageView.xaml";
					break;
				case "guilds":
					uri = "/Views/GuildsPageView.xaml";
					break;
				case "items":
					uri = "/Views/ItemsPageView.xaml";
					break;
				case "arenaTeams":
					uri = "/Views/ArenaTeamsPageView.xaml";
					break;
				case "arenaLadder":
					uri = "/Views/ArenaLadderPageView.xaml";
					break;
				case "ratedBgLadder":
					uri = "/Views/RatedBgLadderPageView.xaml";
					break;
				case "search":
					uri = "/Views/SearchPageView.xaml";
					break;
			}

			this.Frame.Navigate(uri, host);
		}
	}
}
